<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use think\Db;
use think\Model;
use think\Request;
use think\Validate;


class Order extends BaseAdmin
{

    public function index(Request $request)
    {
        if (request()->isAjax()){
            return \app\admin\model\Order::getlist($request);
        }else{
            $pageno = input('param.pageno', 1);
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }


    private function money($ctime)
    {
        $config = db('config')->find();
        $money = 0;
        //计算天数
        $timediff = time()-$ctime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);

        if($days==0){
            if($hours > 10){
                $money = $config['repair_10'];
            }
        }
        else if($days>0){
            $money = $config['repair_10'] + $days*$config['repair_24'];
        }

        $ret['price'] = $money;
        $ret['date'] = $days.'天'.$hours.'时'.$mins.'分';
        return $ret;
    }


    /**
     * 订单列表
     * @return mixed
     */
//    public function index(){
//        if (request()->isAjax()){
//            $list = $this->search('order', true, [], 'order_id desc');
//            if($list){
//                foreach ($list['lists'] as $key => $value) {
//                    $sdate = '';
//                    if($value['type']==2 && $value['status']==2){
//                        $m = $this->money($value['ctime']);
//                        $sdate = $m['date'];
//                    }
//                    $list['lists'][$key]['sdate'] = $sdate;
//                }
//            }
//            return $list;
//        }else{
//            $pageno = input('param.pageno', 1);
//            $this->assign('pageno', $pageno);
//            return $this->fetch();
//        }
//    }



    private function makeOrderNumber() {
        $order_sn = (intval(date('Y')) - 2010) . date('m') . date('d') . substr(time() , -5) . substr(microtime() , 2, 4);
        return str_pad($order_sn, 12, rand(0, 9));
    }

    /**
    **修改添加
    **/
    public function add(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();
                $rule = [
                    ['name','require','磨刀姓名不能为空'],
                    ['type','require|in(1,2)','磨刀类型不能为空'],
                ];
                $validate = new Validate($rule);
                $result   = $validate->check($post);

                if($result===false){
                    return ['status'=>false,'message'=>$validate->getError()];
                }
                
                $post['order_no'] = $this->makeOrderNumber();
                if($post['basic_price']) $post['basic_price']   = $post['basic_price']*100;
                if($post['repair_price']) $post['repair_price'] = $post['repair_price']*100;
                if($post['total_price']) $post['total_price']   = $post['total_price']*100;
                $res = Db::name('order')->insertGetId($post);
                if($res){
                    return ['status'=>true, 'message'=>'新增成功'];
                }else{
                    return ['status'=>false,'message'=>'新增失败'];
                }
                
            }
        }else{
            return $this->fetch();
        }
    }


    /**
    *修改
    **/
    public function set(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();
                if(empty($post)){
                    return ['status'=>false,'message'=>'无数据可修改'];
                }
                
                $id = !empty($post['order_id'])?$post['order_id']:0;
                if($id){
                    unset($post['order_id']);
                    if($post['basic_price']) $post['basic_price']   = $post['basic_price']*100;
                    if($post['repair_price']) $post['repair_price'] = $post['repair_price']*100;
                    if($post['total_price']) $post['total_price']   = $post['total_price']*100;
                    $res = Db::name('order')->where('order_id', $id)->update($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'更新成功'];
                    }else{
                        return ['status'=>false,'message'=>'更新失败'];
                    }
                }else{
                   return ['status'=>false,'message'=>'请求不合法'];
                }
            }else{
                $id = request()->get('id');
                $order = Db::name('order')->where('order_id', $id)->find();
                if($order['basic_price']) $order['basic_price']   = $order['basic_price']/100;
                if($order['repair_price']) $order['repair_price'] = $order['repair_price']/100;
                if($order['total_price']) $order['total_price']   = $order['total_price']/100;
                return $order;
            }
        }else{
            $this->assign('id',request()->get('id'));
            return $this->fetch();
        }
    }



    /**
     * 删除
     * @return array
     */
    public function remove(){
        $ids = request()->post('ids');
        if(request()->isAjax()){
            $ids = json_decode($ids, true);
            $result = Db::name('order')->delete($ids);
            if($result){
                return ['status'=>true,'message'=>'删除成功!'];
            }else{
                return ['status'=>false,'message'=>'删除失败!'];
            }
        }else{
            new \HttpException(502,'请求不合法');
        }
    }



    /**
    **操作订单 
    **/
    public function examine()
    {
        $post = input('post.');
        if(empty($post['order_id'])){
            return ['status'=>false,'message'=>'参数失效!'];
        }

        $info = db('order')->where('order_id', $post['order_id'])->find();
        if(empty($info)){
            return ['status'=>false,'message'=>'数据无效'];
        }

        if($post['type']==1){
            if($info['status']==4){
                return ['status'=>false,'message'=>'订单已完成'];
            }else if($info['status']==3){
                return ['status'=>false,'message'=>'订单已发货'];
            }else if($info['status']==1){
                return ['status'=>false,'message'=>'订单未支付'];
            }
            $status = 3;
        }

        if($post['type']==2){
            if($info['status']==4){
                return ['status'=>false,'message'=>'订单已完成'];
            }else if($info['status']==2){
                return ['status'=>false,'message'=>'订单未发货'];
            }else if($info['status']==1){
                return ['status'=>false,'message'=>'订单未支付'];
            }
            $status = 4;
        }

        $res = db('order')->where('order_id', $post['order_id'])->update(['status'=>$status]);
        if($res!==false){
            return ['status'=>true,'message'=>'操作成功!'];
        }else{
            return ['status'=>false,'message'=>'操作失败!'];
        }

    }



    

}